package com.jlnk.androidfmc;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


public class MessageActivity extends AppCompatActivity {

  private LinearLayout msgContainer;
  private View textViewConsultation;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_message);
    textViewConsultation = findViewById(R.id.consultation_info);
    msgContainer = findViewById(R.id.msg_container);
    // 设置系统窗口 insets 监听器
    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
      Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
      v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
      return insets;
    });
    // 处理从其他活动传递的数据
    handleIntentData();
  }

  private void handleIntentData() {
    Intent intent = getIntent();
    String expertName = intent.getStringExtra("EXPERT_NAME");
    String consultationContent = intent.getStringExtra("CONSULTATION_CONTENT");

    // 如果有传递的数据，创建一个新的LinearLayout并添加到msgContainer中
    if (expertName != null && consultationContent != null) {
      addMessageItem(expertName, consultationContent);
    }
  }

  private void addMessageItem(String expertName, String consultationContent) {
    LayoutInflater inflater = getLayoutInflater();
    LinearLayout messageItem = (LinearLayout) inflater.inflate(R.layout.item_message, msgContainer, false);

    // 确保item_message布局中的TextView ID与下面调用的ID匹配
    TextView textViewExpertName = messageItem.findViewById(R.id.consultation_title);
    TextView textViewConsultationTime = messageItem.findViewById(R.id.consultation_time);
    TextView textViewConsultationInfo = messageItem.findViewById(R.id.consultation_info);

    if (textViewExpertName != null && textViewConsultationTime != null && textViewConsultationInfo != null) {
      textViewExpertName.setText(expertName);
      textViewConsultationTime.setText("刚刚"); // 这里可以设置为当前时间或其他逻辑
      textViewConsultationInfo.setText(consultationContent);
      msgContainer.addView(messageItem);
    } else {
      // 如果任何一个TextView为null，记录日志或处理错误
      Log.e("MessageActivity", "One of the TextViews is null in item_message layout.");
    }
  }


  private LinearLayout createMessageItem(String expertName, String consultationContent) {
    LinearLayout messageItem = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_message, msgContainer, false);

    // 假设您的message_item布局中包含以下TextView
    TextView textViewExpertName = messageItem.findViewById(R.id.consultation_title);
    TextView textViewConsultationTime = messageItem.findViewById(R.id.consultation_time);
    TextView textViewConsultationInfo = messageItem.findViewById(R.id.consultation_info);

    textViewExpertName.setText(expertName);
    textViewConsultationTime.setText("刚刚"); // 这里可以根据需要设置时间
    textViewConsultationInfo.setText(consultationContent);

    return messageItem;
  }


  private void init() {
    initViews();
  }

  private void initViews() {
    msgContainer = findViewById(R.id.msg_container);
    msgContainer.setOnClickListener(v -> {
      startActivity(new Intent(MessageActivity.this, ChatActivity.class));
    });
  }
}